<?php

namespace App\Services\Index;

use App\Constants\CodeData;
use App\Dao\AnchorDao;
use App\Utils\LoggerUtil;
use App\Utils\RedisClient;
use function Hyperf\Translation\trans;

class FateService
{

    /**
     * Note - 缘分推荐弹出用户数据
     * DateTime 2024/8/13 10:52
     * User Admin
     * @param $userInfo
     * @param $language
     * @return array
     */
    public function layer($userInfo, $language)
    {
        $uid = $userInfo['id'];
        if ($userInfo['sex'] == 2) {
            // \Tool::JSON(\CodeData::NEED_TIPS, ['result' => 0], 'success');
            return formatData(['result' => 0], 200, CodeData::NO_PROMPT);
        }

        $redis = RedisClient::getInstance();
        $sortedSetKey = "fate_layer_" . $uid;
        // 获取过期字段
        $currentTime = time();
        $expiredFields = $redis->zRangeByScore($sortedSetKey, 0, $currentTime);
        foreach ($expiredFields as $expiredField) {
            // 删除过期的用户ID
            $redis->zRem($sortedSetKey, $expiredField);
        }
        $uidArr = $redis->zRange($sortedSetKey, 0, -1) ?: [];
        LoggerUtil::write('fate', $uidArr);
        // $uids = implode(',', $uidArr);
        // $uids = $redis->get("fate_layer_".$uid);
        $user = AnchorDao::getInstance()->getRecommendAnchorOne($uidArr);
        if (empty($user)) {
            $user = AnchorDao::getInstance()->getRecommendAnchorOne();
        }
        // if (!empty($uidArr)) {
        //     $user = AnchorDao::getInstance()->getRecommendAnchorOne($uidArr);
        //     if (empty($user)) {
        //         $user = AnchorDao::getInstance()->getRecommendAnchorOne();
        //     }
        // } else {
        //     $user = AnchorDao::getInstance()->getRecommendAnchorOne();
        // }
        if (empty($user)) {
            return formatData(['result' => 0], 200, CodeData::NO_PROMPT);
        }

        // 计算过期时间戳
        $expireTime = time() + 48 * 60 * 60; // 设置字段的过期时间为48小时后
        // 使用有序集合来模拟过期时间
        $redis->zAdd($sortedSetKey, $expireTime, $user['id']);
        $informationLang = trans('information', [], $language);
        $constellationLang = trans('constellation', [], $language);
        $user['job'] = isset($informationLang['job'][$user['job']]) ? $informationLang['job'][$user['job']]['name'] : '';
        $user['constellation'] = $constellationLang[$user['constellation']] ?? '';
        $text = $user['age'] . trans('message.岁', [], $language) . "，" . $user['height'] . "cm，" . $user['constellation'] . "，" . $user['job'];
        $user['text'] = trim($text, '，');
        unset($user['lng'], $user['lat'], $user['ext']);
        if (empty($user['signature'])) {
            $signs = trans('fate', [], $language);
            $user['signature'] = $signs[array_rand($signs)];
        }

        // \Tool::JSON(\CodeData::SUCCESS, ['result' => 1, 'msg' => $user, 'count' => 3, 'time_interval' => 3 * 60], 'success');
        return formatData(['result' => 1, 'msg' => $user, 'count' => 3, 'time_interval' => 180], 200, CodeData::SUCCESS);
    }
}